package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.interceptor.AttributeKeys;
import com.happy3w.ssp.schoolmatehome.service.auth.TheUserDetail;
import com.happy3w.ssp.schoolmatehome.service.note.NoteDetail;
import com.happy3w.ssp.schoolmatehome.service.note.NoteSearcher;
import com.happy3w.ssp.schoolmatehome.service.note.NoteService;
import com.happy3w.ssp.schoolmatehome.service.note.NoteSummary;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/me/note")
@PreAuthorize("hasRole('ROLE_NAMED')")
public class MeNoteController {
    private final NoteService noteService;
    private final NoteSearcher noteSearcher;

    @PostMapping
    public ResponseEntity<NoteDetail> create(@RequestBody NoteDetail note,
                                             @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        NoteDetail createdNote = noteService.createNote(note, userDetail.getUser());
        return ResponseEntity.ok(createdNote);
    }

    @PutMapping("/{id}")
    public ResponseEntity<NoteDetail> update(@PathVariable Long id,
                                       @RequestBody NoteDetail note,
                                             @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        NoteDetail updatedNote = noteService.updateNote(id, note, userDetail.getUser());
        if (updatedNote == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(updatedNote);
    }

    @GetMapping
    public List<NoteSummary> findAllSelf(
            @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail,
            @RequestParam(required = false, name = "query") String query,
            @RequestParam(required = false, name = "page", defaultValue = "0") int page,
            @RequestParam(required = false, name = "size", defaultValue = "20") int size) {
        return noteSearcher.findAllSelf(userDetail.getUser(), query, page, size);
    }

    @GetMapping("/{id}")
    public ResponseEntity<NoteDetail> findById(@PathVariable Long id,
                                               @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        Optional<NoteDetail> noteOpt = noteService.findById(id, userDetail.getUser());
        return noteOpt.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id,
                                       @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        noteService.deleteById(id, userDetail.getUser());
        return ResponseEntity.noContent().build();
    }
}